#include "mainwidget.h"
#include<QHBoxLayout>
#include<QPushButton>
#include <QTimer>
#include"modle/data.h"
#include"debug.h"
#include"sessionfriendarea.h"
#include"messageshowarea.h"
#include"messageeditarea.h"
#include<QSplitter>
#include"modle/data.h"
#include"personinforshow.h"
#include"privatechatsesiondetail.h"
#include"groupchatsessiondetail.h"
#include"addfrienddialog.h"
using namespace module;

MainWidget* MainWidget::instance=nullptr;
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    this->setMinimumSize(930,650);
    this->setWindowTitle("童年对话室");
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));

    this->setStyleSheet(
        "MainWidget {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "}"
        );




    initMainWindow();
    initLeftWindow();
    initMidWindow();
    initRightWindow();
    initSignalAndSlot();
}

MainWidget::~MainWidget() {}

MainWidget *MainWidget::getInstance()
{
    if(instance!=nullptr)
    {
        return instance;
    }
    else
    {
        instance =new MainWidget();
    }
    return instance;
}

void MainWidget::initMainWindow()
{

    left=new QWidget(this);
    mid=new QWidget(this);
    right=new QWidget(this);

    left->setFixedWidth(80);
    left->setStyleSheet(QString("background-color:rgb(35,35,35)"));
    left->setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #1a1a2e, stop:1 #16213e);");


    mid->setFixedWidth(250);
    mid->setStyleSheet(QString("background-color:rgb(42,42,42)"));
    mid->setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #1e1e2e, stop:1 #242442);");

    right->setMinimumWidth(600);
    right->setMinimumHeight(650);
    right->setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #1a1a2a, stop:1 #1e1e32);");


    //主窗口创建布局管理器
    QHBoxLayout *layout=new QHBoxLayout(this);
    //设置内部空间的间隔
    layout->setSpacing(0);
    //设置外边距
    layout->setContentsMargins(0,0,0,0);

    layout->addWidget(left);
    layout->addWidget(mid);
    layout->addWidget(right);

    this->setLayout(layout);

}

void MainWidget::initLeftWindow()
{

    //创建我们的垂直布局管理器
    QVBoxLayout *layout=new QVBoxLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(10, 20, 10, 20);
    layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    avatorbutn=new QPushButton();
    sessionbutn=new QPushButton();
    friendbutn=new QPushButton();
    applybutn=new QPushButton();

    // 设置按钮固定尺寸 - 头像按钮稍大但保持方形
    avatorbutn->setFixedSize(55, 55);      // 头像按钮尺寸
    sessionbutn->setFixedSize(50, 50);     // 功能按钮尺寸
    friendbutn->setFixedSize(50, 50);      // 功能按钮尺寸
    applybutn->setFixedSize(50, 50);       // 功能按钮尺寸

    // 设置图标 默认程序启动就是我们的会话列表就被选中
    //其余不是被选中的
    avatorbutn->setIcon(QIcon(":/Resources/avator1.jpg"));
    sessionbutn->setIcon(QIcon(":/Resources/activemessage.png"));
    friendbutn->setIcon(QIcon(":/Resources/inactivefriend.png"));
    applybutn->setIcon(QIcon(":/Resources/inactiveapply.png"));

    // 设置图标尺寸
    QSize iconSize(30, 30);
    avatorbutn->setIconSize(iconSize);
    sessionbutn->setIconSize(iconSize);
    friendbutn->setIconSize(iconSize);
    applybutn->setIconSize(iconSize);

    // 更新按钮样式 - 更加活泼现代
    QString buttonStyle =
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "               stop:0 rgba(255, 255, 255, 0.1),"
        "               stop:1 rgba(255, 255, 255, 0.05));"
        "   border: none;"
        "   border-radius: 15px;"
        "   padding: 5px;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "               stop:0 rgba(255, 107, 107, 0.3),"
        "               stop:1 rgba(6, 214, 160, 0.3));"
        "   border: 2px solid rgba(255, 255, 255, 0.2);"  // 替代 transform 效果
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "               stop:0 rgba(255, 107, 107, 0.5),"
        "               stop:1 rgba(6, 214, 160, 0.5));"
        "}";




    // 应用统一样式到所有按钮
    avatorbutn->setStyleSheet(buttonStyle);
    sessionbutn->setStyleSheet(buttonStyle);
    friendbutn->setStyleSheet(buttonStyle);
    applybutn->setStyleSheet(buttonStyle);

    // 添加到布局
    layout->addWidget(avatorbutn, 0, Qt::AlignHCenter);
    layout->addWidget(sessionbutn, 0, Qt::AlignHCenter);
    layout->addWidget(friendbutn, 0, Qt::AlignHCenter);
    layout->addWidget(applybutn, 0, Qt::AlignHCenter);

    layout->addStretch();

    left->setLayout(layout);
}

void MainWidget::initMidWindow()
{

    QGridLayout *layout = new QGridLayout();

    layout->setSpacing(0);
    layout->setContentsMargins(10, 0, 0, 0);
    layout->setAlignment(Qt::AlignTop);

    searchLine = new QLineEdit();
    searchLine->setFixedHeight(60);
    searchLine->setPlaceholderText("🔍 搜索");

    QString lineStyle =
        "QLineEdit {"
        "   background: rgba(45, 45, 65, 0.9);"  // 深色半透明背景
        "   border: 1px solid rgba(255, 255, 255, 0.15);"
        "   border-radius: 12px;"
        "   padding: 10px 12px;"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   margin: 10px 5px;"
        "   selection-background-color: rgba(255, 107, 107, 0.3);"  // 选中文本背景色
        "}"
        "QLineEdit:focus {"
        "   border: 2px solid rgba(255, 107, 107, 0.6);"  // 加粗边框替代阴影
        "   background: rgba(55, 55, 75, 0.95);"  // 焦点时稍微改变背景
        "}"
        "QLineEdit::placeholder {"
        "   color: rgba(255, 255, 255, 0.4);"
        "   font-style: italic;"
        "}";
    searchLine->setStyleSheet(lineStyle);

    plusSignal = new QPushButton();
    plusSignal->setFixedSize(35, 35); // 设置固定尺寸，保持正方形
    plusSignal->setIcon(QIcon(":/Resources/plus.png"));
    plusSignal->setIconSize(QSize(25, 25)); // 图标尺寸稍小一些

    plusSignal->setObjectName("plusSignal");
    plusSignal->setStyleSheet(
        "#plusSignal {"
        "   background: transparent;"
        "   border: none;"
        "   border-radius: 8px;"
        "}"
        "#plusSignal:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #06d6a0, stop:1 #26b0e0);"
        "}"
        "#plusSignal:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #04b080, stop:1 #0c7a9c);"
        "}");

    //自定义类进行我们的列表的展示
    SessionFriendArea*showList=new SessionFriendArea(this);
    layout->addWidget(searchLine, 0, 0, 1, 1);
    layout->addWidget(plusSignal, 0, 1, 1, 1);
    layout->addWidget(showList,1,0,1,5);

    // 设置列比例，让搜索框占据更多空间
    layout->setColumnStretch(0, 1); // 第一列可拉伸
    layout->setColumnStretch(1, 0); // 第二列固定宽度



    mid->setLayout(layout);
}

void MainWidget::initRightWindow()
{

    //垂直布局管理器
    QVBoxLayout *layout=new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    layout->setAlignment(Qt::AlignTop);
    right->setLayout(layout);

    //1.添加显示消息基本信息的控件
    QWidget* first=new QWidget();
    first->setFixedHeight(80);
    first->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    first->setObjectName("firstwidget");
    first->setStyleSheet(
        "#firstwidget {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 rgba(255, 107, 107, 0.1),"
        "               stop:0.5 rgba(255, 209, 102, 0.05),"
        "               stop:1 rgba(6, 214, 160, 0.1));"
        "   border-bottom: 1px solid rgba(255, 255, 255, 0.1);"
        "   border-top: 3px solid qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "                       stop:0 #ff6b6b, stop:0.5 #ffd166, stop:1 #06d6a0);"
        "}");
    //第一个我们的上方标题
    QHBoxLayout *firstlayout=new QHBoxLayout();
    firstlayout->setContentsMargins(0,0,0,0);
    firstlayout->setSpacing(0);
    first->setLayout(firstlayout);
    //用于显示的label
    QLabel *title =new QLabel();
    title->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
#if TEST_UI
    title->setText("童年对话室");
#endif
    // 美化标题
    title->setStyleSheet(
        "QLabel {"
        "   color: #ffd166;"
        "   font-size: 18px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "   padding: 8px 15px;"
        "   border-radius: 8px;"
        "}");


    //三个点的按钮
    pointBtn=new QPushButton();
    pointBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    pointBtn->setText("...");
    pointBtn->setFixedSize(32, 32);  // 固定尺寸
    pointBtn->setStyleSheet(
        "QPushButton {"
        "   background: rgba(255, 255, 255, 0.1);"
        "   border: none;"
        "   border-radius: 8px;"
        "   color: #ffd166;"
        "   font-size: 16px;"
        "   font-weight: bold;"
        "   padding: 5px 10px;"
        "}"
        "QPushButton:hover {"
        "   background: rgba(255, 107, 107, 0.3);"
        "   color: white;"
        "}"
        "QPushButton:pressed {"
        "   background: rgba(255, 107, 107, 0.5);"
        "}");

    //添加第一个控件的元素
    firstlayout->addWidget(title,0,Qt::AlignCenter|Qt::AlignLeft);
    firstlayout->addWidget(pointBtn,0,Qt::AlignCenter|Qt::AlignRight);

    //创建消息展示区
    MessageShowArea* secondMessShowArea=new MessageShowArea();


#ifdef TEST_UI
    bool isleft=true;
    int index=0;
    for(int i=1;i<=20;i++)
    {
        QVector<QString>strs(2);
        strs[0]="这是一条消息";
        strs[1]="这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                  "这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                  "这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息这是一条测试消息"
                  "这是一条测试消息这是一条测试消息这是一条测试消息";
        User user;
        user.name="张三"+QString::number(i);
        user.avatar=QIcon(":/Resources/avator1.jpg");
        Message mess=Message::makeMessage(MessageType::TEXT_TYPE,"1",user,strs[index].toUtf8(),"") ;
        index=(index+1)%2;
        secondMessShowArea->addMessageItem(isleft,mess);
        if(i%2==0)
            isleft=!isleft;
    }

    // QTimer *timer=new QTimer(this);
    // timer->setInterval(2000); // 1秒
    // //注意这这里我们捕获的时候要用值捕捉
    // //如果引用捕捉由于定时任务之后执行的
    // //当我们的Initright函数执行之后此时我们
    // //secondMessShowArea指针变量会被销毁
    // //注意此时只是局部变量被销毁，不是它所
    // //指向的空间被销毁了。而如果进行引用捕捉
    // //引用不会存储指针变量，而是直接使用secondMessShowArea指针变量
    // //此时secondMessShowArea已经销毁了，此时的引用就是空引用
    // connect(timer,&QTimer::timeout,secondMessShowArea,[=](){
    //     secondMessShowArea->clear();
    //     timer->stop();
    // });
    // timer->start();
#endif
    //创建消息编辑区域
    MessageEditArea* thirdMessEditArea=new MessageEditArea();

    // 使用QSplitter来实现可调节高度
    QSplitter *splitter = new QSplitter(Qt::Vertical);
    splitter->setChildrenCollapsible(false); // 防止子部件被完全折叠
    splitter->setHandleWidth(1); // 设置分割条宽度
    // 设置分割条的样式
    splitter->setStyleSheet(
        "QSplitter::handle {"
        "   background-color: rgba(128, 128, 128, 0.3);"
        "   height: 1px;"
        "}"
        "QSplitter::handle:hover {"
        "   background-color: rgba(128, 128, 128, 0.6);"
        "}");

    // 将消息显示区和编辑区添加到分割器
    splitter->addWidget(secondMessShowArea);
    splitter->addWidget(thirdMessEditArea);

    // 设置初始比例（例如：消息显示区占70%，编辑区占30%）
    QList<int> sizes;
    sizes << 700 << 300; // 根据实际需要调整这些值
    splitter->setSizes(sizes);
    //把第一个控件加入整体控件
    layout->addWidget(first,0,Qt::AlignTop);
    layout->addWidget(splitter, 1);
}





//连接信号和槽函数
void MainWidget::initSignalAndSlot()
{
    connect(avatorbutn,&QPushButton::clicked,this,&MainWidget::handlerAvator);
    connect(sessionbutn,&QPushButton::clicked,this,&MainWidget::handlerSession);
    connect(friendbutn,&QPushButton::clicked,this,&MainWidget::handlerFriend);
    connect(applybutn,&QPushButton::clicked,this,&MainWidget::handlerApply);
    connect(pointBtn,&QPushButton::clicked,this,[](){
        bool isgroupsessiondetail=true;
        if(isgroupsessiondetail)
        {
            GroupChatSessionDetail *wid=new GroupChatSessionDetail("人类吃喝研究小组");
#ifdef TEST_UI
            QString name1="张三是哈哈哈哈哈大王啊";
            QString name2="张三";
            QIcon icon(":/Resources/avator1.jpg");
            bool tem=true;
            for(int i=0;i<1;i++)
            {
                if(tem)
                {
                    wid->addIemToScrollArea(icon,QString::number(i+1)+name1);
                    wid->addIemToScrollArea(icon,QString::number(i+1)+name2);
                    wid->addIemToScrollArea(icon,QString::number(i+1)+name1);
                }
                else
                {
                    wid->addIemToScrollArea(icon,QString::number(i+1)+name2);
                    wid->addIemToScrollArea(icon,QString::number(i+1)+name1);
                }
                tem=!tem;
            }

#endif
            wid->exec();
        }
        else
        {
            QDialog*wid=new PrivateChatSesionDetail();
            wid->exec();
        }
    });



    connect(plusSignal,&QPushButton::clicked,this,[=](){
        AddFriendDialog*dialog=new AddFriendDialog(this);
        dialog->exec();
    });
}



//左边栏按钮的槽函数实现
void MainWidget::handlerAvator()
{
#if TEST_UI
    PersonInforShow *perinfor=new PersonInforShow;
    perinfor->exec();
#endif

}

void MainWidget::handlerSession()
{
    LOG()<<"点击了会话按钮";
    selectStatus=SESSION_LIST;
    sessionbutn->setIcon(QIcon(":/Resources/activemessage.png"));
    friendbutn->setIcon(QIcon(":/Resources/inactivefriend.png"));
    applybutn->setIcon(QIcon(":/Resources/inactiveapply.png"));

}

void MainWidget::handlerFriend()
{
    LOG()<<"点击了好友按钮";
    selectStatus=FRIEDN_LIST;
    sessionbutn->setIcon(QIcon(":/Resources/inactivemessage.png"));
    friendbutn->setIcon(QIcon(":/Resources/activefriend.png"));
    applybutn->setIcon(QIcon(":/Resources/inactiveapply.png"));
}

void MainWidget::handlerApply()
{
    LOG()<<"点击了好友申请按钮";
    selectStatus=APPLY_LIST;
    sessionbutn->setIcon(QIcon(":/Resources/inactivemessage.png"));
    friendbutn->setIcon(QIcon(":/Resources/inactivefriend.png"));
    applybutn->setIcon(QIcon(":/Resources/activeapply.png"));
}
